﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using DAL;
using Model;
using System.Security.Cryptography;

namespace BLL
{
    public class AdminService
    {
        DBHelper db = new SqlServerDBHelper();


        public string EncryptWithMD5(string source)
        {
            byte[] sor = Encoding.UTF8.GetBytes(source);
            MD5 md5 = MD5.Create();
            byte[] result = md5.ComputeHash(sor);
            StringBuilder strbul = new StringBuilder(40);
            for (int i = 0; i < result.Length; i++)
            {
                strbul.Append(result[i].ToString("x2"));

            }
            return strbul.ToString().ToLower();
        }

        public AdminInfo Login(string name, string pass)
        {
            string sql = string.Format(@"select * from AdminInfo 
where AdminName='{0}' and AdminPass='{1}'", name, EncryptWithMD5(pass));
            Console.WriteLine(sql);
            DbDataReader dr = db.ExecuteReader(sql);
            AdminInfo admin = null;
            if (dr.Read())
            {
                admin = new AdminInfo();
                admin.AdminId = Convert.ToInt32(dr["AdminId"]);
                admin.AdminName = dr["AdminName"].ToString();
                admin.AdminPass = dr["AdminPass"].ToString();
                admin.AdminRole = Convert.ToInt32(dr["AdminRole"]);
                admin.AdminStatus = Convert.ToInt32(dr["AdminStatus"]);
            }
            dr.Close();
            db.Close();
            return admin;
        }
    }
}
